using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;


public delegate void bpDragEvent(PointerEventData eventData);

/// <summary>
/// 背包管理
/// </summary>
public class BackpackManager : MonoBehaviour
{

    public int packNum = 24;

    public GameObject preGird; // 格子预制
    public Transform tranGird; // 挂点
    public BackpackDragItem dragItem;
    public Transform tranItem;
    public BackpackGird[,] girds;
    private List<Vector2> needCheckRowCol = new List<Vector2>();
    private List<BackpackGird> lastGird = new List<BackpackGird>();  // 拖拽时检测的格子

    void Start()
    {
        InitGrid();
        InitTempItem();
    }

    /// <summary>
    /// 初始化背包格子
    /// </summary>
    private void InitGrid()
    {
        // [0,0]....[0,3] 
        // ....
        // [5,0]....[5,3]
        girds = new BackpackGird[6,4];
        if (preGird && tranGird)
        {
            int row = 0; // 行
            int col = 0; // 列
            for (int i = packNum -1; i >= 0; i--)
            {
                var grid = GameObject.Instantiate(preGird, tranGird);
                grid.SetActive(true);
                grid.name = "grid_" + (packNum -i);
                var packGrid = grid.transform.GetComponent<BackpackGird>();
                row = Mathf.FloorToInt(i/4.0f);
                col = i% 4;
                girds[row, col] = packGrid;
                packGrid.InitView(row,col,this);
            }
        }
    }

    /// <summary>
    /// 初始化临时物品 真正要用的时候剥离数据
    /// </summary>
    private void InitTempItem()
    {
        if (tranItem)
        {
            for (int i = 0; i < tranItem.childCount; i++)
            {
                var item = tranItem.GetChild(i).GetComponent<BackpackItem>();
                if (item)
                {
                    item.InitView(this);
                }
            }
        }
    }

    /// <summary>
    ///检测当前选中的格子
    /// </summary>
    /// <param name="eventData"></param>
    /// <returns></returns>
    public BackpackGird CheckGrid(PointerEventData eventData)
    {
        if (eventData.pointerCurrentRaycast.gameObject)
        {
            var target = eventData.pointerCurrentRaycast.gameObject;
            if (target)
            {
                BackpackGird grid = target.transform.GetComponent<BackpackGird>();
                if (grid)
                {
                    return grid;
                }
            }
        }
        return null;
    }

    /// <summary>
    /// 检测物品能不能塞下
    /// </summary>
    public void CheckSetItem()
    {

    }

    /// <summary>
    /// 设置拖拽 位置 物品
    /// </summary>
    /// <param name="eventData"></param>
    /// <param name="item"></param>
    private void SetDragItem(PointerEventData eventData, BackpackItem item = null)
    {
        if (dragItem)
        {
            dragItem.SetPos(eventData.position);
            if (item)
            {
                dragItem.SetItem(item);
            }
        }
    }

    #region 具体物品拖拽逻辑
    public void StartDrag(PointerEventData eventData, BackpackItem item)
    {
        this.SetDragItem(eventData, item);
        var grid = CheckGrid(eventData);
        if (grid)
        {
            grid.IsDisable();
        }
    }

    public void InDrag(PointerEventData eventData, BackpackItem item)
    {
        this.SetDragItem(eventData);
        var grid = CheckGrid(eventData);
        if (grid)
        {
            CheckDragToGird(item, grid);
        }
    }

    public void EndDrag(PointerEventData eventData, BackpackItem item)
    {
        this.EndDragItem(item);
    }

    /// <summary>
    /// 拖拽物品结束
    /// </summary>
    private void EndDragItem(BackpackItem item)
    {
        //位置不够的时候 不塞入
        if (lastGird.Count < item.GetNeedGrid())
        {
            foreach (var gird in lastGird)
            {
                gird.SetDefault();
            }
            lastGird.Clear();

            if (dragItem)
            {
                dragItem.SetItem();
            }
            return;
        }

        // 物品塞入左上角 重置数据
        if (lastGird.Count > 0)
        {
            int minRow = 5;
            int minCol = 3;
            int curRow;
            int curCol;
            foreach (var gird in lastGird)
            {
                gird.SetDefault();
                gird.GetRowCol(out curRow, out curCol);
                if (curRow < minRow)
                {
                    minRow = curRow;
                }
                if (curCol < minCol)
                {
                    minCol = curCol;
                }
            }
            var needgrid = girds[minRow, minCol];
            if (needgrid.HasItem() == false)
            {
                needgrid.SetItem(dragItem.GetItem());
                foreach (var gird in lastGird)
                {
                    gird.SaveItemGird(needgrid);
                    gird.SetItem();
                }
            }
            lastGird.Clear();
            if (dragItem)
            {
                dragItem.SetItem();
            }
        }
    }



    /// <summary>
    /// 拖拽物品时检测背包格子
    /// </summary>
    /// <param name="item"></param>
    /// <param name="grid"></param>
    private void CheckDragToGird(BackpackItem item,BackpackGird grid)
    {
        needCheckRowCol.Clear();
        foreach (var g in lastGird)
        {
            g.SetDefault();
        }
        lastGird.Clear();
        int itemCurRow, itemCurCol;
        int itemRow, itemCol;
        int girdRow, girdCol;
        item.GetCurSelectRowCol(out itemCurRow, out itemCurCol);
        item.GetItemRowCol(out itemRow, out itemCol);
        grid.GetRowCol(out girdRow,out girdCol);

        // 先考虑当前挪进来的，挪出去的再考虑
        for (int i = 0; i < itemRow; i++)
        {
            for (int j = 0; j < itemCol; j++)
            {
                if (i != itemCurRow || j != itemCurCol)
                {
                    Vector2 change = new Vector2( i - itemCurRow,j - itemCurCol);
                    needCheckRowCol.Add(change);
                }
            }   
        }

        int maxRow = 5;
        int minRow = 0;
        int maxCol = 3;
        int minCol = 0;
        int row, col;
        girds[girdRow, girdCol].CheckHasItem();
        lastGird.Add(girds[girdRow, girdCol]);
        for (int i = 0; i < needCheckRowCol.Count; i++)
        {
            row = girdRow + (int)needCheckRowCol[i].x;
            col = girdCol + (int)needCheckRowCol[i].y;
            if (row <= maxRow && row >= minRow && col >= minCol && col <= maxCol)
            {
                var g = girds[row, col];
                g.CheckHasItem();
                if (g.HasItem() == false)
                {
                    lastGird.Add(g);
                }
            }
        }
    }


    #endregion

    #region 背包格子拖拽逻辑
    // 主要拖出去
    // 交换待考虑


    public void StartDragGrid(PointerEventData eventData,BackpackGird grid)
    {
        // 保存了物品格子
        var curItemGird = grid.GetSaveItemGird();
        if (curItemGird)
        {
            if (curItemGird.GetItem())
            {
                this.SetDragItem(eventData, curItemGird.GetItem());
                this.CheckRemoveItemGird(curItemGird);
                curItemGird.RemoveItem();
            }
        }
    }


    public void InDragGrid(PointerEventData eventData, BackpackGird grid)
    {
        this.SetDragItem(eventData);
    }

    public void EndDragGrid(PointerEventData eventData, BackpackGird grid)
    {
        // todo 拖出去就直接移除了，交互等逻辑需要再补
        if (dragItem)
        {
            dragItem.SetItem();
        }
    }

    /// <summary>
    /// 检测需要移除物品的格子
    /// </summary>
    private void CheckRemoveItemGird(BackpackGird grid)
    {
        needCheckRowCol.Clear();
        var item = grid.GetItem();
        int itemCurRow = 0, itemCurCol = 0;
        int itemRow, itemCol;
        int girdRow, girdCol;
        item.GetItemRowCol(out itemRow, out itemCol);
        grid.GetRowCol(out girdRow, out girdCol);

        for (int i = 0; i < itemRow; i++)
        {
            for (int j = 0; j < itemCol; j++)
            {
                if (i != itemCurRow || j != itemCurCol)
                {
                    Vector2 change = new Vector2(i - itemCurRow, j - itemCurCol);
                    needCheckRowCol.Add(change);
                }
            }
        }
        int row, col;
        for (int i = 0; i < needCheckRowCol.Count; i++)
        {
            row = girdRow + (int)needCheckRowCol[i].x;
            col = girdCol + (int)needCheckRowCol[i].y;
           
            var g = girds[row, col];
            g.RemoveItem();
        }
        needCheckRowCol.Clear();

    }
    #endregion
}
